<template>
  <div>
    <div>【服务资源占用比】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
import * as echarts from "echarts";
const props = defineProps({
  data: {
    type: Object,
    required: true
  }
});
console.log("【竖向柱状图】", props.data);

// 1. 初始化echarts实例
let mChart = null;
const target = ref(null);

onMounted(() => {
  mChart = echarts.init(target.value);

  renderChart();
});

// 2. 构建option配置对象
const renderChart = () => {
  const options = {
    // X轴展示数据
    xAxis: {
      type: "category",
      data: props.data.servers.map(item => item.name),
      axisLabel: {
        //y轴文字的配置
        color: "#9EB1C8"
        //  color: "white" //Y轴内容文字颜色
      }
    },
    // Y轴展示数据
    yAxis: {
      show: false,
      type: "value",
      max: function(value) {
        return parseInt(value.max * 1.2);
      }
    },
    // 图表绘制的位置，对应 上下左右
    grid: {
      top: 16,
      right: 0,
      bottom: 26,
      left: -26,
      containLabel: true
    },
    //核心配置
    series: [
      {
        type: "bar",
        data: props.data.servers.map(item => ({
          name: item.name,
          value: item.value
        })),
        showBackground: true,
        backgroundStyle: {
          color: "rgba(180,180,180,0.2)"
        },
        itemStyle: {
          color: "#479AD3",
          //   barBorderRadius: 5,
          shadowColor: "rgba(0,0,0,0.3)",
          shadowBlur: 5
        },
        barWidth: 12,
        label: {
          show: true,
          position: "top",
          color: "#fff",
          formatter: "{c}%"
        }
      }
    ]
  };

  // 3.通过实例.setOptions(option)
  if (mChart != null) {
    mChart.setOption(options);
  }
};
watch(
  () => props.data,
  () => {
    renderChart();
  }
);
</script>

<style lang="scss" scoped>
</style>